﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Back.Xinkui.DataAccess.Company;
using CommonFramework.Utility;
using Back.Xinkui.Entity.Company;
using Back.Xinkui.WebModel;
using Xinkui.Common.Enum;
using Back.Xinkui.Common.Enum;
using Back.Xinkui.Common.Control.DataDictionary;
using Xinkui.Common.FileAction;

public partial class company_detail : PageAspxBase
{
    protected int CompanyID
    {
        get
        {
            return WebHelper.GetQueryStringValue<int>("companyId", 0);
        }
    }

    protected int AuditStatus { get; set; }

    /// <summary>
    /// 是否买家
    /// </summary>
    protected bool IsBuy
    {
        get
        {
            return WebHelper.GetQueryStringValue<int>("isBuy", 0) == 1;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ExprotMarket = DataDictionaryManager.DataDictionary.Where(u => u.TypeCode == (int)EDictType.MainBuyingArea).ToList();
        if (!IsPostBack)
        {
            BindCompanyInfo();
        }
    }

    protected string imgCompanyLogo;
    protected string imgCompanyImage;

    protected void BindCompanyInfo()
    {
        if (CompanyID == 0)
        {
            txtUserName.ReadOnly = false;
            BindDictSource(this.dropDownCapital, EDictType.MonenyRange, -1);
            BindDictSource(this.dropDownExportRatio, EDictType.ExportRatio, -1);
            BindDictSource(this.dropDownNumberOfRDEmployees, EDictType.NumberOfRdEmployees, -1);
            BindDictSource(this.dropDownNumOfEmployees, EDictType.NumberOfEmployees, -1);
            BindDictSource(this.dropDownOwnership, EDictType.OwnerShipType, -1);
            BindDictSource(this.dropDownSales, EDictType.MonenyRange, -1);
            BindDictSource(dropDownJobTitle, EDictType.JobTitle, -1);
            return;
        }

        CompanyDetailEntity company = CompanyDataAccess.GetCompanyInfoByCompanyID(CompanyID);
        if (company != null)
        {
            ApsotoRPC1.SelectCountry = company.CountryID;
            ApsotoRPC1.SelectProvince = company.ProvinceID;
            ApsotoRPC1.SelectCity = company.CityID;

            this.txtAddress.Text = company.StreetAddress;
            this.txtCnCompanyName.Text = company.CnCompanyName;
            this.txtCompanyDesc.Text = company.CompanyDescription;
            this.txtCompanyEmail.Text = company.CompanyEmail;
            this.txtCompanyFax.Text = company.FaxNum;
            this.txtCompanyName.Text = company.CompanyName;
            this.txtCompanyPhone.Text = company.PhoneNum;
            this.txtMainProduct.Text = company.MainProduct;
            this.txtPostCode.Text = company.PostCode;
            this.txtWebSite.Text = company.WebSite;

            imgCompanyLogo = BuliderImage(company.Logo, 100, 100, ESourceType.FromCompanyLogo);
            viewLogo.ImageUrl = FileFactory.BrowerFile(company.Logo, ESourceType.FromCompanyLogo, 100, 100);
            hidLogo.Value = company.Logo;

            imgCompanyImage = BuliderImage(company.ImagePhoto, 100, 100, ESourceType.FromCompanyImage);
            viewImage.ImageUrl = FileFactory.BrowerFile(company.ImagePhoto, ESourceType.FromCompanyImage, 100, 100);
            hidImage.Value = company.ImagePhoto;

            //BindDictSource(this.DropJobTitle, EDictType.JobTitle, userInfo.JobTitle);
            BindDictSource(this.dropDownCapital, EDictType.MonenyRange, company.RegisterCapital);
            BindDictSource(this.dropDownExportRatio, EDictType.ExportRatio, company.ExportRatio);
            BindDictSource(this.dropDownNumberOfRDEmployees, EDictType.NumberOfRdEmployees, company.NumberofRDEmployees);
            BindDictSource(this.dropDownNumOfEmployees, EDictType.NumberOfEmployees, company.NumberofEmployees);
            BindDictSource(this.dropDownOwnership, EDictType.OwnerShipType, company.OwnershipType);
            BindDictSource(this.dropDownSales, EDictType.MonenyRange, company.TotalAnnualSales);
            //BindDictSource(this.dropDownIsExport, EDictType.IsExport, company.IsExport);
            this.chkIsExport.Checked = company.IsExport == 1;

            this.txtCeo.Text = company.Ceo;
            //this.txtExprotMarket.Text = company.ExportMarket;



            ExprotMarket.ForEach(u =>
            {
                if (company.ExportMarket.SplitString(",").Contains(u.Id.ToString()))
                {
                    u.IsChecked = true;
                }
            });

            this.txtTypical.Text = company.TypicalClients;
            this.txtYear.Text = company.YearEstabished;

            chkIsOE.Checked = company.IsOE == 1;

            UserEntity user = CompanyDataAccess.GetCompanyUserByCompanyID(company.CompanyID);
            if (user != null)
            {
                txtFirstName.Text = user.FirstName;
                txtLastName.Text = user.LastName;
                txtUserName.Text = user.UserName;
                txtUserEmail.Text = user.Email;
                txtUserMobile.Text = user.Mobile;
                txtDepartment.Text = user.Department;
                BindDictSource(dropDownJobTitle, EDictType.JobTitle, user.JobTitle);

                if (user.Gender == 2)
                {
                    this.rbtnMale.Checked = true;
                }
                if (user.Gender == 1)
                {
                    this.rbtnFemale.Checked = true;
                }

            }

            #region 绑定证书信息

            this.rpCertList.DataSource = CompanyDataAccess.GetCompanyCertificateByCompanyID(company.CompanyID);
            this.rpCertList.DataBind();


            this.rpEquipmentList.DataSource = CompanyDataAccess.GetCompanyEquipmentByCompanyID(company.CompanyID);
            this.rpEquipmentList.DataBind();
            #endregion
            AuditStatus = company.AuditStatus;
        }
    }

    protected List<DataDictionaryeEntity> ExprotMarket
    {
        get;
        set;
    }


    protected void BindDictSource(DropDownList dropDown, EDictType t, int value)
    {
        var dictValues = DataDictionaryManager.DataDictionary.Where(u => u.TypeCode == (int)t).ToList();
        if (dictValues == null)
        {
            dictValues = new List<DataDictionaryeEntity>();
            dictValues.Add(new DataDictionaryeEntity
            {
                Id = -1,
                TypeCode = (int)t,
                Name = "----Please Select----"
            });
        }
        dropDown.DataSource = dictValues;
        dropDown.DataTextField = "Name";
        dropDown.DataValueField = "Id";
        dropDown.DataBind();
        dropDown.Items.Insert(0, new ListItem
        {
            Text = "----Please Select----",
            Value = "-1"
        });
        if (dictValues.Count(u => u.Id == value) > 0)
        {
            dropDown.SelectedValue = value.ToString();
        }
        else
        {
            dropDown.SelectedIndex = 0;
        }
    }
    /// <summary>
    /// 审核通过
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnApprove_Click(object sender, EventArgs e)
    {
        CompanyDataAccess.UpdateCompanyStatus(CompanyID, (int)EAuditStatus.Approve, WebHelper.UserIP);
        ShowAndReloadOpner("审核通过");
    }
    /// <summary>
    /// 审核拒绝
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnReject_Click(object sender, EventArgs e)
    {
        CompanyDataAccess.UpdateCompanyStatus(CompanyID, (int)EAuditStatus.Reject, WebHelper.UserIP);
        ShowAndReloadOpner("操作成功");
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        //获取表单数据
        CompanyDetailEntity company = GetPostCompanyInfo();
        UserEntity user = GetPostUserInfo();
        if (CompanyID > 0)
        {
            //编辑
            company.CompanyID = CompanyID;
            company.UpdateDate = DateTime.Now;
            UserEntity dbUser = CompanyDataAccess.GetCompanyUserByCompanyID(company.CompanyID);
            if (dbUser != null)
            {
                user.UserID = dbUser.UserID;
            }
            //保存
            CompanyDataAccess.SaveCompanyInfo(company, user);
            ShowAndReloadOpner("保存成功");
        }
        else
        {
            //新增
            company.CreateIp = WebHelper.UserIP;
            user.Password = "123456";
            company.MemberType = IsBuy ? (short)100 : (short)0;
            CompanyDataAccess.CreateCompanyInfo(company, user);
            ShowAndReloadOpner("添加成功");
        }


    }

    /// <summary>
    /// 获取表单公司信息
    /// </summary>
    /// <returns></returns>
    private CompanyDetailEntity GetPostCompanyInfo()
    {
        CompanyDetailEntity company = new CompanyDetailEntity();

        company.CountryID = ApsotoRPC1.SelectCountry;
        company.ProvinceID = ApsotoRPC1.SelectProvince;
        company.CityID = ApsotoRPC1.SelectCity;

        company.StreetAddress = this.txtAddress.Text;
        company.CompanyDescription = this.txtCompanyDesc.Text;
        company.CompanyEmail = this.txtCompanyEmail.Text;
        company.FaxNum = this.txtCompanyFax.Text;
        company.CompanyName = this.txtCompanyName.Text;
        company.PhoneNum = this.txtCompanyPhone.Text;
        company.MainProduct = this.txtMainProduct.Text;
        company.PostCode = this.txtPostCode.Text;
        company.WebSite = this.txtWebSite.Text;
        company.CnCompanyName = this.txtCnCompanyName.Text;

        company.Logo = hidLogo.Value;
        company.ImagePhoto = hidImage.Value;

        company.RegisterCapital = this.dropDownCapital.SelectedValue.ToType<int>(0);
        company.ExportRatio = this.dropDownExportRatio.SelectedValue.ToType<int>(0);
        company.NumberofRDEmployees = this.dropDownNumberOfRDEmployees.SelectedValue.ToType<int>(0);
        company.NumberofEmployees = this.dropDownNumOfEmployees.SelectedValue.ToType<int>(0);
        company.OwnershipType = this.dropDownOwnership.SelectedValue.ToType<int>(0);
        company.TotalAnnualSales = this.dropDownSales.SelectedValue.ToType<int>(0);

        company.IsExport = this.chkIsExport.Checked ? 1 : 0;
        company.Ceo = this.txtCeo.Text;
        company.ExportMarket = WebHelper.GetFormHtmlValue<string>("chkMarket", string.Empty);

        company.TypicalClients = this.txtTypical.Text;
        company.YearEstabished = this.txtYear.Text;
        company.IsOE = chkIsOE.Checked ? 1 : 0;

        return company;
    }

    /// <summary>
    /// 获取表单用户信息
    /// </summary>
    /// <returns></returns>
    private UserEntity GetPostUserInfo()
    {
        UserEntity user = new UserEntity();

        user.FirstName = txtFirstName.Text;
        user.LastName = txtLastName.Text;
        user.UserName = txtUserName.Text;
        user.Email = txtUserEmail.Text;
        user.Mobile = txtUserMobile.Text;
        user.Department = txtDepartment.Text;

        user.JobTitle = dropDownJobTitle.SelectedValue.ToType<int>(0);

        if (this.rbtnMale.Checked)
        {
            user.Gender = 2;
        }
        if (this.rbtnFemale.Checked)
        {
            user.Gender = 1;
        }

        return user;
    }
}